﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.DataControls;
using Telerik.Web.UI;

public partial class Templates_Files : BaseTemplatePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dsFiles.SelectData += new EventHandler<SelectEventArgs<TemplateFile>>(dsFiles_SelectData);
        lnkNew.Click += new EventHandler(lnkNew_Click);
        dsFiles.UpdatingData += new EventHandler<UpdatingDataEventArgs<TemplateFile>>(dsFiles_UpdatingData);
        dsFiles.UpdateData += new EventHandler<UpdateDataEventArgs<TemplateFile>>(dsFiles_UpdateData);
        grid.ItemDataBound += new GridItemEventHandler(grid_ItemDataBound);
        if (!IsPostBack)
        {
            this.Title = string.Format("{0} Template Files", base.CurrentTemplate.Name);
        }
    }

    public override void GetBreadCrumbs(BaseMasterPage.BreadCrumbCollection breadcrumbs)
    {
        base.GetBreadCrumbs(breadcrumbs);
        if(CurrentTemplate != null)
        {
            breadcrumbs.Add("Files");
        }
    }

    private void HideDelete(Control c)
    {
        if (c is LinkButton && ((LinkButton)c).CommandName.ToLower() == "delete")
        {
            c.Visible = false;
            return;
        }
        foreach (Control child in c.Controls)
        {
            HideDelete(child);
        }
    }

    void grid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        //if default dont allow deletes
        if (e.Item is GridDataItem)
        {
            TemplateFile file = e.Item.DataItem as TemplateFile;
            if(file != null && file.IsDefault)
            {
                HideDelete(e.Item);
            }
        }
    }

    void dsFiles_UpdateData(object sender, Acms.DataControls.UpdateDataEventArgs<TemplateFile> e)
    {
        bool commitTran = false;
        if (e.Item.IsDefault)
        {
            foreach (GridDataItem row in grid.MasterTableView.Items)
            {
                if ((bool)row.GetDataKeyValue("IsDefault"))
                {
                    commitTran = true;
                    e.Item.BeginTransaction();
                    TemplateFile oldDefault = TemplateFile.GetById((int)row.GetDataKeyValue("Id"));
                    if (oldDefault != null && oldDefault.Id != e.Item.Id)
                    {
                        oldDefault.IsDefault = false;
                        oldDefault.UseTransaction(e.Item.TransactionKey);
                        oldDefault.Update();
                    }
                }
            }
        }
        e.Item.Update();
        if (commitTran)
        {
            e.Item.CommitTransaction();
        }
    }

    void dsFiles_UpdatingData(object sender, Acms.DataControls.UpdatingDataEventArgs<TemplateFile> e)
    {
        if (e.Action == Acms.DataControls.UpdateAction.Insert)
        {
            e.Item.TemplateId = base.TemplateId;
        }
    }

    void lnkNew_Click(object sender, EventArgs e)
    {
        grid.EditIndexes.Clear();
        grid.MasterTableView.IsItemInserted = true;
        grid.Rebind();
    }

    void dsFiles_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.TemplateFile> e)
    {
        e.SetData(TemplateFile.GetAllByTemplateId(TemplateId));
    }
}
